package com.nus.fellowmark.config;

//import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.nus.fellowmark.modules.rbac.enums.RoleEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Ruoyi Chen
 * @email chenruoyisz@gmail.com
 * @create 2023/2/11
 */
//@Configuration
@Slf4j
public class WebConfigurer implements WebMvcConfigurer {
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // open router auth check
//        registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> {
//            log.info(">>> complete path of the request: {}", request.getUrl());
//            // login interceptor
//            SaRouter.match("/**")
//                    .notMatch(notLogin())
//                    .check(StpUtil::checkLogin);
//
//            // important paths that need to check auth permission
//            SaRouter.match(authPermission())
//                    .notMatch(notAuth())
//                    .check(() -> StpUtil.checkRole(RoleEnum.AUTH.getCode()));
//        }));
//    }

    /**
     * add paths that can be accessed without login
     * @return
     */
    public static List<String> notLogin() {

        List<String> allows = new ArrayList<>();

        allows.add("/user/register");
        allows.add("/user/login");

        // allow swagger resources
        allows.add("/v2/**");
        allows.add("/v3/**");
        allows.add("/swagger-ui.html/**");
        allows.add("/swagger-ui/**");
        allows.add("/doc.html/**");
        allows.add("/api-docs-ext/**");
        allows.add("/swagger-resources/**");
        allows.add("/webjars/**");
        allows.add("/favicon.ico");
        allows.add("/error");

        return allows;
    }

    /**
     * add paths that need to check auth permission
     * @return
     */
    public static List<String> authPermission() {

        List<String> allows = new ArrayList<>();

        // TODO: add paths

        return allows;
    }

    /**
     * add paths that no need to check auth permission, but need to login
     * @return
     */
    public static List<String> notAuth() {
        List<String> allows = new ArrayList<>();

        // TODO: add paths

        return allows;
    }
}
